<html>
<head><meta charset="utf-8"><title>zeroing out padding · project-safe-transmute · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/index.html">project-safe-transmute</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html">zeroing out padding</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201328418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201328418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201328418">(Jun 18 2020 at 21:30)</a>:</h4>
<p>Here's an interesting project that zeroes out padding on structs, and so makes it legal to cast them into bytes: <a href="https://crates.io/crates/safe-bytes">https://crates.io/crates/safe-bytes</a></p>



<a name="201353494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201353494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201353494">(Jun 19 2020 at 05:21)</a>:</h4>
<p>Shouldn't there be a language construct for this? The proc-macro in this crate must ensure that all bytes are SafeBytes and do not contain any padding of their own. This is very infectious. Are there any known cases where this is too restrictive?</p>



<a name="201353556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201353556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201353556">(Jun 19 2020 at 05:22)</a>:</h4>
<p>The language construct might need to be similar to <code>packed</code>, access to fields is <code>unsafe</code>. Otherwise you'd be able to write to fields in their own type which would interact with the layout semantics of those types again.</p>



<a name="201353588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201353588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201353588">(Jun 19 2020 at 05:23)</a>:</h4>
<p>There's some discussion about zeroed padding on an unsafe working group issue. (I don't have the link handy though.)</p>



<a name="201353686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201353686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201353686">(Jun 19 2020 at 05:25)</a>:</h4>
<p>I find one closed issue: <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/174">https://github.com/rust-lang/unsafe-code-guidelines/issues/174</a></p>



<a name="201353726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201353726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201353726">(Jun 19 2020 at 05:26)</a>:</h4>
<p>And one open, but restricted to union padding: <a href="https://github.com/rust-lang/rust/issues/70230">https://github.com/rust-lang/rust/issues/70230</a></p>



<a name="201384019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201384019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201384019">(Jun 19 2020 at 12:45)</a>:</h4>
<p>The biggest issue is that as soon as you perform a Move/Copy of the value, the padding bytes aren't logically preserved and so they go back to being uninit.</p>



<a name="201393669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201393669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201393669">(Jun 19 2020 at 14:00)</a>:</h4>
<p>That's why it would need to be a language construct :)</p>



<a name="201398861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201398861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201398861">(Jun 19 2020 at 14:41)</a>:</h4>
<p>It feels like that would take runtime tracking too, which sounds poor</p>



<a name="201405076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201405076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201405076">(Jun 19 2020 at 15:26)</a>:</h4>
<p>That's what I meant with the observation of it being unsafe to access fields. If you're not allowed to write fields in their own types then I don't think it does. A write would simply preserve the guarantee zero-padding, i.e. it could either copy the padding from the other instance with guaranteed padding or it could skip writing it. That's in contrast to normal padding where it may additionally write any other value or uninit.</p>



<a name="201408770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/zeroing%20out%20padding/near/201408770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/zeroing.20out.20padding.html#201408770">(Jun 19 2020 at 15:55)</a>:</h4>
<p>Well there's always memcopy I suppose</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>